package com.wzh.forxu;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.wzh.forxu.adapter.ConsumeAdapter;
import com.wzh.forxu.base.BaseActivity;
import com.wzh.forxu.model.ConsumeDetail;
import com.wzh.forxu.utils.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @Author Hu
 * @Time 2017/4/21 -- 16:43.
 * @Email 87683202@qq.com
 */
public class MainActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.day)
    TextView day;
    @BindView(R.id.week)
    TextView week;
    @BindView(R.id.month)
    TextView month;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    @BindView(R.id.add_ConsumeDetail)
    Button addConsumeDetail;
    ConsumeAdapter consumeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        myRecy.setLayoutManager(new LinearLayoutManager(context));
        consumeAdapter = new ConsumeAdapter(R.layout.item_consume, new ArrayList<ConsumeDetail>());
        myRecy.setAdapter(consumeAdapter);
        Observable<JSONObject> observable = accountBookService.getListAccount();
        observable.subscribeOn(Schedulers.io())  // 网络请求切换在io线程中调用
                .unsubscribeOn(Schedulers.io())// 取消网络请求放在io线程
                .observeOn(AndroidSchedulers.mainThread())// 观察后放在主线程调用
                .subscribe(new Consumer<JSONObject>() {
                    @Override
                    public void accept(@NonNull JSONObject jsonObject) throws Exception {
                        List<ConsumeDetail> consumeDetailList = JsonUtil.getListFromString(String.valueOf(jsonObject.getJSONArray("res")), ConsumeDetail.class);
                        consumeAdapter.setNewData(consumeDetailList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        Log.e("-----err===", throwable.toString());
                    }
                });

    }

    @OnClick(R.id.add_ConsumeDetail)
    public void onViewClicked() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}
